# 用户类
class User:
    id = ""
    # 用户属性
    name = ""          # 姓名
    code = ""          # 编码
    phone = 0          # 电话
    passwd = ""        # 密码
    loginTime = ""     # 登录时间
    createTime = ""    # 创建时间
    updateTime = ""    # 更新时间

    # 构造方法，初始化用户对象
    def __init__(self, name):
        self.name = name

    # 静态方法，从字典解码用户信息
    @staticmethod
    def decode(user_dict):
        user = User(user_dict['name'])
        user.phone = user_dict['phone']
        user.passwd = user_dict['passwd']
        return user

    # 生成插入 SQL 语句
    def generate_insert_sql(self):
        return "INSERT INTO user(username, code, password, role, phone, create_time, update_time) " \
               "VALUES('{}', '{}', '{}', '1', '{}','{}', '{}')"\
            .format(self.name, self.code, self.passwd, self.phone, self.createTime, self.updateTime)

    # 静态方法，生成查询所有用户的 SQL 语句
    @staticmethod
    def generate_list_sql():
        return "SELECT * FROM user"

    # 静态方法，生成按姓名查询用户的 SQL 语句
    @staticmethod
    def generate_find_by_name_sql(name):
        return "SELECT * FROM user WHERE username = '{}'".format(name)

    # 静态方法，生成按姓名删除用户的 SQL 语句
    @staticmethod
    def generate_delete_by_name_sql(name):
        return "DELETE FROM user WHERE username = '{}'".format(name)

    # 生成更新用户信息的 SQL 语句
    def generate_update_sql(self):
        sql_builder = ""
        if self.code != "":
            sql_builder += " code = '{}',".format(self.code)
        if self.passwd != "":
            sql_builder += " password = '{}',".format(self.passwd)
        if self.phone != "":
            sql_builder += " phone = '{}',".format(self.phone)
        if self.loginTime != "":
            sql_builder += " login_time = '{}',".format(self.loginTime)
        if self.createTime != "":
            sql_builder += " create_time = '{}',".format(self.createTime)
        if self.updateTime != "":
            sql_builder += " update_time = '{}',".format(self.updateTime)

        if sql_builder != "":
            sql_builder = "UPDATE user SET {} WHERE username = '{}'".format(sql_builder[:len(sql_builder) - 1], self.name)
            return sql_builder
